/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.domain.MOIData;
import com.carnation.mong.platform.domain.MOIModel;
import com.carnation.mong.platform.service.Query;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class QueryImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private Query service;

	public QueryImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(Query.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.carnation.qa.socketserver.proxy.AbstractServiceImpl#invoke(com.carnation
	 * .qa.socketserver.domain.Item)
	 */
	@Override
	protected String invoke() {
		String returnStr = null;
		boolean needClose = true;
		for (Item i : getItems()) {
			String method = getMethodName(i);
			if ("close".equals(method)) {
				close();
				needClose = false;
			} else if ("folder".equals(method)) {
				folder(i);
			} else if ("get".equals(method)) {
				returnStr = get(i);
			} else if ("go".equals(method)) {
				go();
			} else if ("model".equals(method)) {
				model(i);
			} else if ("name".equals(method)) {
				name(i);
			} else if ("size".equals(method)) {
				returnStr = size(i);
			} else {
				throw new IllegalArgumentException(method);
			}
		}
		if (needClose) {
			// close();
		}
		return returnStr;
	}

	private void folder(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIData i = instanceMOIData(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("folder folder:" + i);
			}
			try {
				service.folder(i);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(e.getMessage(), e);
				}
			}
		} else {
			throw new IllegalArgumentException(item.toString());
		}
	}

	private void model(Item item) {
		if (item.getChildren().size() == 2) {// 参数个数
			MOIModel i = instanceMOIModel(item.getChildren().get(0));
			Map<String, Object> attrNameValueMap = instanceStringObjectMap(item
					.getChildren().get(1));
			if (log.isDebugEnabled()) {
				log.debug("model m:" + i + ",attrNameValueMap:"
						+ attrNameValueMap);
			}
			try {
				service.model(i, attrNameValueMap);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(e.getMessage(), e);
				}
			}
		} else {
			throw new IllegalArgumentException(item.toString());
		}
	}

	private void name(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			String name = getString(item.getChildren().get(0).getLabelParts()[1]);
			Boolean isCaseSensitive = getBoolean(item.getChildren().get(1)
					.getLabelParts()[1]);
			Boolean isMatchWholeWord = getBoolean(item.getChildren().get(2)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("name name:" + name + ",isCaseSensitive:"
						+ isCaseSensitive + ",isMatchWholeWord:"
						+ isMatchWholeWord);
			}
			try {
				service.name(name, isCaseSensitive, isMatchWholeWord);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(e.getMessage(), e);
				}
			}
		} else {
			throw new IllegalArgumentException(item.toString());
		}
	}

	private void go() {
		if (log.isDebugEnabled()) {
			log.debug("go");
		}
		try {
			service.go();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(e.getMessage(), e);
			}
		}
	}

	private void close() {
		if (log.isDebugEnabled()) {
			log.debug("close");
		}
		try {
			service.close();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(e.getMessage(), e);
			}
		}
	}

	private String get(Item item) {
		if (item.getChildren().size() == 2) {// 参数个数
			int first = getInteger(item.getChildren().get(0).getLabelParts()[1]);
			int size = getInteger(item.getChildren().get(1).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("get first:" + first + ",size:" + size);
			}
			try {
				Object obj = service.get(first, size);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String size(Item item) {
		try {
			Object obj = service.size();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

}
